package com.bbkb.quartz.service.impl;


import com.bbkb.common.base.exception.BaseCheckException;
import com.bbkb.quartz.constants.ScheduleConstants;
import com.bbkb.quartz.dao.SysJobMapper;
import com.bbkb.quartz.domain.SysJob;
import com.bbkb.quartz.dto.SysJobDTO;
import com.bbkb.quartz.service.ISysJobService;
import com.bbkb.quartz.task.RyTask;
import com.bbkb.quartz.util.PageResultUtil;
import com.bbkb.quartz.util.QuartzConvert;
import com.bbkb.quartz.util.ScheduleUtils;
import com.bbkb.quartz.vo.page.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ArrayUtils;
import org.quartz.JobDataMap;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;

import javax.annotation.PostConstruct;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @author nobi
 */
@Service
public class ISysJobServiceImpl implements ISysJobService {

    @Autowired
    private Scheduler scheduler;
    @Autowired
    private SysJobMapper sysJobMapper;

    @PostConstruct
    public void init() throws SchedulerException, BaseCheckException {
        scheduler.clear();
        List<SysJob> jobList = sysJobMapper.selectByAll(null);
        for (SysJob job : jobList) {
            ScheduleUtils.createScheduleJob(scheduler, job);
        }

    }

    public static void main(String[] args) {
        Method[] allDeclaredMethods = ReflectionUtils.getDeclaredMethods(RyTask.class);
        System.out.println(allDeclaredMethods);
    }

    @Override
    public void addSysJob(SysJobDTO sysJobDTO) throws BaseCheckException {
        SysJob sysJob = QuartzConvert.sysJobDto2domain(sysJobDTO);
        sysJobMapper.insertSelective(sysJob);
    }

    @Override
    public void updateSysJob(SysJobDTO sysJobDTO) throws BaseCheckException {
        SysJob sysJob = QuartzConvert.sysJobDto2domain(sysJobDTO);
        sysJobMapper.updateByPrimaryKeySelective(sysJob);
    }

    @Override
    public SysJobDTO getSysJob(Long jobId) throws BaseCheckException {
        SysJob sysJob = sysJobMapper.selectByPrimaryKey(jobId);
        return QuartzConvert.sysJob2Dto(sysJob);
    }

    @Override
    public PageResult<SysJobDTO> queryPage(SysJobDTO jobDTO) throws BaseCheckException {
        PageHelper.startPage(jobDTO.getPage(), jobDTO.getRows());
        List<SysJob> list = sysJobMapper.selectByAll(jobDTO);
        return PageResultUtil.convert(new PageInfo<>(list), QuartzConvert::sysJob2Dto);
    }

    @Override
    public void run(Long id) throws SchedulerException {
        SysJob sysJob = sysJobMapper.selectByPrimaryKey(id);
        // 参数
        JobDataMap dataMap = new JobDataMap();
        dataMap.put(ScheduleConstants.TASK_PROPERTIES, sysJob);
        scheduler.triggerJob(ScheduleUtils.getJobKey(id, sysJob.getJobGroup()), dataMap);
    }

    @Override
    @Transactional
    public void deleteByIds(Long[] ids) throws BaseCheckException, SchedulerException {
        if (ArrayUtils.isEmpty(ids)) {
            throw new BaseCheckException("ids不可为空");
        }
        for (Long jobId : ids) {
            SysJob job = sysJobMapper.selectByPrimaryKey(jobId);
            if (sysJobMapper.deleteByPrimaryKey(job.getId()) > 0) {
                scheduler.deleteJob(ScheduleUtils.getJobKey(job.getId(), job.getJobGroup()));
            }
        }
    }

}
